package com.rayboot.footpoint.utils;

import java.io.File;
import java.text.DecimalFormat;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.StatFs;

public class StorageUtil {
	public static final int ACTION_DEVICE_STORAGE_LOW = 7;
	public static final int ACTION_DEVICE_STORAGE_OK = 8;
	public static final int ACTION_MEDIA_MOUNTED = 1;
	public static final int ACTION_MEDIA_SHARED = 6;
	public static final int ACTION_MEDIA_UNMOUNTED = 2;
	private static final int CANNOT_STAT_ERROR = -2;
	private static final int NO_STORAGE_ERROR = -1;
	private static final String PATH;
	public static int StorageCounter;
	private static String TAG = "StorageUtil";
	private static boolean mInternalStorageFull;
	private static long mRemainingStroage;
	public boolean isReg = false;
	private Context mContext;
	private int mCurrentState = 0;
	private OnChangeStorageListener mOnChangeStorageListener = null;
	private BroadcastReceiver mReceiver;

	static {
		PATH = Environment.getDataDirectory().getPath();
		StorageCounter = 0;
		mInternalStorageFull = false;
	}

	public StorageUtil(Context context) {
		this.mContext = context;
		// this.mReceiver = new BroadcastReceiver() {
		// public void onReceive(Context paramContext, Intent paramIntent) {
		// String str = paramIntent.getAction();
		// if (str.equals("android.intent.action.MEDIA_MOUNTED"))
		// StorageUtil.this.setState(1);
		// while (true) {
		// return;
		// if (str.equals("android.intent.action.MEDIA_UNMOUNTED")) {
		// StorageUtil.this.setState(2);
		// continue;
		// }
		// if (str.equals("android.intent.action.MEDIA_SHARED")) {
		// StorageUtil.this.setState(6);
		// continue;
		// }
		// if (str.equals("android.intent.action.DEVICE_STORAGE_LOW")) {
		// StorageUtil.access$102(true);
		// continue;
		// }
		// if (!str.equals("android.intent.action.DEVICE_STORAGE_OK"))
		// continue;
		// StorageUtil.access$102(false);
		// }
		// }
		// };
	}

	private static String formatSize(long paramLong) {
		String str1 = null;
		if (paramLong >= 1024L) {
			str1 = " KB";
			paramLong /= 1024L;
			if (paramLong >= 1024L) {
				str1 = " MB";
				paramLong /= 1024L;
			}
		}
		DecimalFormat localDecimalFormat = new DecimalFormat();
		localDecimalFormat.setGroupingSize(3);
		String str2 = localDecimalFormat.format(paramLong);
		if (str1 != null)
			str2 = str2 + str1;
		return str2;
	}

	private static long getFileSize(File paramFile) {
		return 0;
		// long l1 = 0L;
		// long l2;
		// if (paramFile == null)
		// l2 = 0L;
		// while (true)
		// {
		// return l2;
		// if (paramFile.isFile())
		// {
		// l2 = paramFile.length();
		// continue;
		// }
		// File[] arrayOfFile = paramFile.listFiles();
		// int i = arrayOfFile.length;
		// for (int j = 0; j < i; j++)
		// l1 += getFileSize(arrayOfFile[j]);
		// l2 = l1;
		// }
	}

	public static String getFootprintStorageDirectory() {
		return Environment.getExternalStorageDirectory().getAbsolutePath()
				+ File.separator + ".footprints";
	}

	private IntentFilter initFilter() {
		IntentFilter localIntentFilter = new IntentFilter(
				"android.intent.action.MEDIA_MOUNTED");
		localIntentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
		localIntentFilter.addAction("android.intent.action.MEDIA_SHARED");
		localIntentFilter.addAction("android.intent.action.DEVICE_STORAGE_LOW");
		localIntentFilter.addAction("android.intent.action.DEVICE_STORAGE_OK");
		localIntentFilter.addDataScheme("file");
		return localIntentFilter;
	}

	public static boolean isMediaShared() {
		return Environment.getExternalStorageState().equals("shared");
	}

	private void setState(int state) {
		this.mCurrentState = state;
		this.mOnChangeStorageListener.onStorageChanged(state);
	}

	public void SetOnChangeStorageListerner(
			OnChangeStorageListener paramOnChangeStorageListener) {
		this.mOnChangeStorageListener = paramOnChangeStorageListener;
	}

	public void closeStorgaeUtil() {
		if ((this.mReceiver != null) && (this.isReg == true)) {
			this.mContext.unregisterReceiver(this.mReceiver);
			this.isReg = false;
			StorageCounter -= 1;
		}
	}

	public String getExternalStorageDirectory() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	public void startStorageUtil() {
		if (!this.isReg) {
			this.mContext.registerReceiver(this.mReceiver, initFilter());
			this.isReg = true;
			StorageCounter = 1 + StorageCounter;
		}
	}

	public static abstract interface OnChangeStorageListener {
		public abstract void onStorageChanged(int state);
	}

	public static boolean isExternalStorageEnough() {
		return availableStorage() > FootpointConstant.EXTERNAL_MEMORY_THRESHOLD;
	}

	public static boolean isExternalStorageEnough(long requestSize) {
		return availableStorage() > requestSize;
	}

	public static boolean isInternalStorageEnough() {
		return checkIternalMemory() <= FootpointConstant.INTERNAL_MEMORY_THRESOLD;
	}

	public static boolean isInternalStorageEnough(long requestSize) {
		return checkIternalMemory() <= requestSize;
	}

	public static long availableStorage() {
		if (!hasExternalStorage()) {
			mRemainingStroage = -1L;
		} else {
			StatFs localStatFs = new StatFs(Environment
					.getExternalStorageDirectory().getPath());
			mRemainingStroage = localStatFs.getBlockSize()
					* localStatFs.getAvailableBlocks();
		}
		return mRemainingStroage;
	}

	public static boolean hasExternalStorage() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	public static long checkIternalMemory() {
		StatFs localStatFs = new StatFs(PATH);
		return localStatFs.getAvailableBlocks() * localStatFs.getBlockSize();
	}

	public static boolean isStorageEnough() {
		if (isInternalStorageEnough()) {
			return isExternalStorageEnough();
		}
		return false;
	}

}
